package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.POSUnit;

public class POSUnitLogic extends AbstractBL {
	public POSUnitLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<POSUnit> GeneratePOSUnit(long hotelId){
		List<POSUnit> list = new ArrayList<POSUnit>();
		POSUnit unit;
		if(GetPOSUnit(hotelId,"Cup") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Cup");
			unit.setSymbol("Cup");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Time") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Time");
			unit.setSymbol("Time");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Page") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Page");
			unit.setSymbol("Page");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Room") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Room");
			unit.setSymbol("Room");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Bottle") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Bottle");
			unit.setSymbol("Bottle");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Plate") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Plate");
			unit.setSymbol("Plate");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Person") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Person");
			unit.setSymbol("Person");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Unit") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Unit");
			unit.setSymbol("Unit");
			list.add(unit);
		}
		if(GetPOSUnit(hotelId,"Piece") == null){
			unit = new POSUnit();
			unit.setHotelId(hotelId);
			unit.setTitle("Piece");
			unit.setSymbol("Piece");
			list.add(unit);
		}
		return list;
	}
	
	public POSUnit GetPOSUnit(long id)
	{
		Key key = KeyFactory.createKey(EntityName.POSUnit, id);
		try {
			Entity lang = datastore.get(key);
			POSUnit c = new POSUnit();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public POSUnit GetPOSUnit(long hotelId,String title)
	{
		Query q = new Query(EntityName.POSUnit);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetPOSUnit(datastore.prepare(q).asSingleEntity());
	}
	
	public POSUnit GetPOSUnit(Entity entity)
	{
		if(entity == null) return null;
		POSUnit item = new POSUnit();
		item.setEntity(entity);
		return item;
	}
	
	public List<POSUnit> GetPOSUnitList(List<Entity> entities)
	{
		List<POSUnit> list = new ArrayList<POSUnit>();
		for(Entity e : entities)
		{
			POSUnit a = new POSUnit();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public List<POSUnit> GetAllPOSUnitLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.POSUnit);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		q.setFilter(filter);
		return GetPOSUnitList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
		//return GetPOSUnitList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	public List<POSUnit> GetAllPOSUnit(long hotelId)
	{
		Query q = new Query(EntityName.POSUnit);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterPOSUnit = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterPOSUnit);
		
		q.setFilter(filter);
		
		return GetPOSUnitList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}	
	
	public POSUnit AddPOSUnit(long id,String symbol,String title) throws EntityNotFoundException{
		POSUnit a = new POSUnit();
		
		a.setHotelId(id);
		a.setTitle(title);
		a.setSymbol(symbol);
		a.setAvaliable(true);
		a.setStatus(true);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSUnit EditPOSUnit(long UnitId,String symbol,String title) throws EntityNotFoundException{
		POSUnit a = GetPOSUnit(UnitId);
		a.setTitle(title);
		a.setSymbol(symbol);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSUnit ChangeStatus(long amenId) throws EntityNotFoundException{
		
		POSUnit a = GetPOSUnit(amenId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSUnit DeletePOSUnit(long amenId) throws EntityNotFoundException{
			
			POSUnit a = GetPOSUnit(amenId);
			a.setAvaliable(false);
			a.setStatus(false);
			
			Key k = datastore.put(a.getEntity());
			a.setId(k.getId());
			a.setKey(k);
	
			return a;
	}
}
